package com.example.administrator.daydayappmarket.software.choice.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.recommend.activity.RecommendTopImageSpecialThirdActivity;
import com.example.administrator.daydayappmarket.software.choice.model.SoftwareChoiceCarousel;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by lenovo on 2016/12/26.
 */

public class SoftwarePagerAdapter extends PagerAdapter {

    private Context context;
    private List<SoftwareChoiceCarousel> data;

    public SoftwarePagerAdapter(Context context, List<SoftwareChoiceCarousel> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data!=null?500:0;
    }

    @Override
    public View instantiateItem(ViewGroup container, final int position) {

        View ret=null;
        ImageView imageView=new ImageView(context);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));
        imageView.setImageResource(R.mipmap.app_default);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        if(data.get(position%data.size()).getImage().startsWith("http")){
            Glide.with(context).load(data.get(position%data.size()).getImage()).into(imageView);
        }else {
            Glide.with(context).load(Contacts.FRONT_IMG + data.get(position % data.size()).getImage()).into(imageView);
        }
        ret=imageView;

        //设置ViewPager内的图片的点击事件
        imageView.setClickable(true);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String click_type = data.get(position % data.size()).getClick_type();
                String id = data.get(position % data.size()).getId();
                Intent intent=new Intent();
                if(click_type.equals("topic_info")){
                    //跳到众多App推荐页面
                    intent.setClass(context,RecommendTopImageSpecialThirdActivity.class);

                }else if(click_type.equals("app_info")){
                    //跳到App介绍页面
                    intent.setClass(context,RecommendTopImageSpecialThirdActivity.class);
                }
                intent.putExtra("id",id);
                context.startActivity(intent);
            }
        });


        container.addView(ret);




        return ret;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        if (object instanceof View) {
            View view= (View) object;
            container.removeView(view);
        }

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
}
